
// 加载动画库
const ora = require('ora');
// 给提示文案着色
const chalk = require('chalk');

let spinner;
const handleMessage = (msg) => {
    let message = msg;
    if (typeof msg === 'object') try { message = JSON.stringify(message); } catch (error) { }
    return message;
}

module.exports = {
    loading: (message) => {
        if (!spinner) spinner = ora();
        spinner.start();
        spinner.color = 'cyan';
        spinner.text = chalk.cyan(message || 'Loading...');
    },
    stopLoading: () => {
        if (spinner) spinner.stop();
    },
    loadingSucceed: (message) => {
        spinner.succeed(chalk.green(message || 'Succeed!'));
    },
    loadingFailed: (message) => {
        spinner.fail(chalk.red(message || 'Failed!'));
    },
    print: (message) => {
        console.log(chalk.gray(handleMessage(message)));
    },
    printSuccess: (message) => {
        console.log(chalk.green(handleMessage(message)));
    },
    printWarning: (message) => {
        console.log(chalk.yellow(handleMessage(message)));
    },
    printError: (message) => {
        console.log(chalk.red(handleMessage(message)));
    },
};
